
export default {
  
    methods: {
        // 碰撞检测算法
      hitTestRectangle(r1, r2,callBack) {
        //Define the variables we'll need to calculate
        let hit, combinedHalfWidths, combinedHalfHeights, vx, vy;
        //hit will determine whether there's a collision
        hit = false;
        //Find the center points of each sprite
        r1.centerX = r1.x + r1.width / 2;
        r1.centerY = r1.y + r1.height / 2;
        r2.centerX = r2.x + r2.width / 2;
        r2.centerY = r2.y + r2.height / 2;
        //Find the half-widths and half-heights of each sprite
        r1.halfWidth = r1.width / 2;
        r1.halfHeight = r1.height / 2;
        r2.halfWidth = r2.width / 2;
        r2.halfHeight = r2.height / 2;
        //Calculate the distance vector between the sprites
        vx = r1.centerX - r2.centerX;
        vy = r1.centerY - r2.centerY;
        //Figure out the combined half-widths and half-heights
        combinedHalfWidths = r1.halfWidth + r2.halfWidth;
        combinedHalfHeights = r1.halfHeight + r2.halfHeight;
        //Check for a collision on the x axis
        if (Math.abs(vx) < (combinedHalfWidths)+10) {
          //A collision might be occurring. Check for a collision on the y axis
          if (Math.abs(vy) < (combinedHalfHeights)-11) {
            //There's definitely a collision happening
            hit = true;
            if(callBack){
                callBack(vx,vy)
            }
          } else {
            //There's no collision on the y axis
            hit = false;
          }
        } else {
          //There's no collision on the x axis
          hit = false;
        }
        //`hit` will be either `true` or `false`
        return hit;
      },

      // 标尺校准算法
      externalRulerTest(current,list){
        // 精度值
        let accuracy = 8
        // 获取精度值内的x点
        for(let i = 0; i<list.length;i++){
          if(current.name===list[i].name){
            continue
          }
          // 左侧精度校准
          if(Math.abs(current.x-list[i].x)<accuracy){
            current.x = list[i].x
            break
          }
          // 上方精度校准
          if(Math.abs(current.y-list[i].y)<accuracy){
            current.y = list[i].y
            break

          }
          // 右方精度校准
          const currentRight = current.x+current.width
          const listRight = list[i].x+list[i].width
          if(Math.abs(currentRight-listRight)<accuracy){
            current.x = list[i].x+list[i].width-current.width
            break

          }
          // 下方精度校准
          const currentBottom = current.y+current.height
          const listBottom = list[i].y+list[i].height
          if(Math.abs(currentBottom-listBottom)<accuracy){
            current.y = list[i].y+list[i].height-current.height
            break

          }
        }

      },

      // error提示
      alertErrorName(list){
          let errorList = []
					for(let conflictIndex = 0;conflictIndex<list.length;conflictIndex++){
						if(!errorList.includes(list[conflictIndex].errorType)){
							errorList.push(list[conflictIndex].errorType)
						}
					}
					let errorName = ''
					for(let errorIndex = 0;errorIndex<errorList.length;errorIndex++){
						if(errorIndex>0){
							errorName+='、'
						}
						errorName+=errorList[errorIndex]
					}
          return errorName
      },
      // 直接关系错误提示
      alertErrorNameDirect(list){
        let errorList = []
        for(let conflictIndex = 0;conflictIndex<list.length;conflictIndex++){
          if(!errorList.includes(list[conflictIndex].itemType)){
            errorList.push(list[conflictIndex].itemType)
          }
        }
        let errorName = ''
        for(let errorIndex = 0;errorIndex<errorList.length;errorIndex++){
          if(errorIndex>0){
            errorName+='、'
          }
          errorName+=errorList[errorIndex]
        }
        return errorName
    },
      // error三个点文字标红
      errorTitle(list){
        let obj = {
          filterError:false,
          anchorError:false,
          standError:false,
          linkLineError:false,
          linkLineRingError:false,
          instantiationError:false,
        }
        for(let conflictIndex = 0;conflictIndex<list.length;conflictIndex++){
          if(list[conflictIndex].errorType==='过滤设置冲突'){
            obj.filterError = true
          }
          if(list[conflictIndex].errorType==='排序设置冲突'){
            obj.instantiationError = true
          }
          if(list[conflictIndex].errorType==='关系线冲突'||list[conflictIndex].errorType==='锚点设备冲突'){
            obj.anchorError = true
            obj.linkLineError = true
            obj.standError = true
            obj.linkLineRingError = true
          }
        }

        return obj
        
      }



     
    },
  };
     
